package net.valerijtimakov.homefinances.dao.storages.common.mysql.query_builders;

import net.valerijtimakov.homefinances.exceptions.AppExceptionWrongState;

public class MultyUpdateQuery extends MultiInsertQuery {
	
	private String identityFieldName = "";

	@Override
	public String getSql() throws AppExceptionWrongState {
		StringBuilder res = getSqlSb();
		StringBuilder updLine = getFieldsUpdateLine();
		if (res.length() == 0 || updLine.length() == 0) {
			return "";
		}
		res.append(" ON DUPLICATE KEY UPDATE ");
		res.append(updLine);
		res.append(";");
		return res.toString();
	}
	
	private StringBuilder getFieldsUpdateLine() {
		StringBuilder res = new StringBuilder();
		for (String fName : getFieldNamesSet()) {
			if (fName.equals(identityFieldName)) {
				continue;
			}
			if (res.length() > 0) {
				res.append(",");
			}
			res.append(fName);
			res.append("=VALUES(");
			res.append(fName);
			res.append(")");
		}
		return res;
	}

}
